---
title: 设置连接监听
hide_title: true
sidebar_position: 2
---

<Tabs
groupId="sdks-language"
values={[
{ label: 'Android', value: 'andorid', },
{ label: 'iOS', value: 'ios', },
{ label: 'JavaScript', value: 'js', }
]
}>
<TabItem value="andorid">

```java
JetIM.getInstance().getConnectionManager().addConnectionStatusListener("main", new IConnectionManager.IConnectionStatusListener() {
    @Override
    public void onStatusChange(JetIMConst.ConnectionStatus connectionStatus, int i) {
        if (connectionStatus == JetIMConst.ConnectionStatus.CONNECTED) {
            Log.i("TAG", "SDK connect success");
        }
    }

    @Override
    public void onDbOpen() {
        Log.i("TAG", "onDbOpen");
    }
});
```

</TabItem>
<TabItem value="ios">

全局只需设置一次，多次设置会被覆盖。

```objc
[JIM.shared.connectionManager setDelegate:self];

- (void)dbDidOpen {
    NSLog(@"dbDidOpen");
}

- (void)connectionStatusDidChange:(JConnectionStatus)status errorCode:(JErrorCode)code {
		if (JConnectionStatusConnected == status) {
				NSLog(@"SDK connect success");
		}
}
```
</TabItem>
<TabItem value="js">


全局只需设置一次，多次设置会覆盖，除 connect 方法外，其他所有 SDK 方法必须在连接成功后调用，Event 说明请查看 [监听枚举](../enum/web#listener)

```js
let { Event, ConnectionState } = jetim;

jetim.on(Event.STATE_CHANGED, ({ state, user }) => {

  if(ConnectionState.CONNECTING == state){
    console.log('im is connecting');
  }
  
  if(ConnectionState.CONNECTED == state){
    console.log('im is connected', user);
  }
  
  if(ConnectionState.DISCONNECTED == state){
    console.log('im is disconnected');
  }

});

```
</TabItem>
</Tabs>